package application.gui.clientWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.text.DateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import application.CommunicationModule.Communication;
import application.LoginAndAdministrationModule.PersonData;
import application.client.data.Address;
import application.client.data.Client;
import application.parcelModule.Parcel;

public class AddNewParcelPanel extends JPanel {
	public AddNewParcelPanel(ClientHomePage parent, Communication c,
			PersonData p) {
		super();
		setSize(600, 480);
		setLayout(null);
		this.parent = parent;
		this.communication = c;
		this.personData = p;
		init();
		editMode = false;
	}

	private void init() {
		parcel = new Parcel();
		df = DateFormat.getDateInstance();
		sendDate = new JFormattedTextField(df);
		sendDate.setText(df.format(new Date()));
		sendDate.setEnabled(false);
		parcelType = new JComboBox();
		parcelType.addItem("Normalna");
		parcelType.addItem("Priorytet");
		parcelType.addItem("Ekspres");
		sender = new JTextField(personData.getName() + " "
				+ personData.getSurname());
		sender.setEditable(false);
		width = new JTextField();
		height = new JTextField();
		length = new JTextField();
		mass = new JTextField();
		price = new JTextField();
		price.setEditable(false);
		name = new JTextField();
		surname = new JTextField();
		phoneNumber = new JTextField();
		street = new JTextField();
		houseNumber = new JTextField();
		flatNumber = new JTextField();
		city = new JTextField();
		postCode = new JTextField();

		sendDateInfo = new JLabel("Data wysłania");
		parcelTypeInfo = new JLabel("Typ paczki");
		senderInfo = new JLabel("Nadawca");
		widthInfo = new JLabel("Długość paczki");
		heightInfo = new JLabel("Szerokość paczki");
		lengthInfo = new JLabel("Wysokość paczki");
		massInfo = new JLabel("Masa paczki");
		priceInfo = new JLabel("Cena paczki");
		nameInfo = new JLabel("Imię odbiorcy");
		surnameInfo = new JLabel("Nazwisko");
		phoneInfo = new JLabel("Nr telefonu");
		streetInfo = new JLabel("Ulica");
		houseInfo = new JLabel("Nr mieszkania");
		flatInfo = new JLabel("Nr domu");
		cityInfo = new JLabel("Miasto");
		postCodeInfo = new JLabel("Kod pocztowy");
		errorInfo = new JLabel();
		addComponent(errorInfo, 30, 370, 500, 30);

		calculatePrice = new JButton("Oblicz");

		addComponent(sendDateInfo, 10, 20, 120, 30);
		addComponent(sendDate, 135, 20, 100, 30);
		addComponent(parcelTypeInfo, 250, 20, 120, 30);
		addComponent(parcelType, 385, 20, 100, 30);
		addComponent(senderInfo, 10, 55, 120, 30);
		addComponent(sender, 135, 55, 100, 30);
		addComponent(widthInfo, 250, 55, 120, 30);
		addComponent(width, 385, 55, 100, 30);
		addComponent(heightInfo, 10, 90, 120, 30);
		addComponent(height, 135, 90, 100, 30);
		addComponent(lengthInfo, 250, 90, 120, 30);
		addComponent(length, 385, 90, 100, 30);
		addComponent(massInfo, 10, 125, 120, 30);
		addComponent(mass, 135, 125, 100, 30);
		addComponent(priceInfo, 250, 125, 120, 30);
		addComponent(price, 385, 125, 100, 30);
		addComponent(calculatePrice, 500, 125, 70, 30);
		addComponent(nameInfo, 10, 160, 120, 30);
		addComponent(name, 135, 160, 100, 30);
		addComponent(surnameInfo, 250, 160, 120, 30);
		addComponent(surname, 385, 160, 100, 30);
		addComponent(streetInfo, 10, 195, 120, 30);
		addComponent(street, 135, 195, 100, 30);
		addComponent(flatInfo, 250, 195, 120, 30);
		addComponent(flatNumber, 385, 195, 100, 30);
		addComponent(houseInfo, 10, 230, 120, 30);
		addComponent(houseNumber, 135, 230, 100, 30);
		addComponent(cityInfo, 250, 230, 120, 30);
		addComponent(city, 385, 230, 100, 30);
		addComponent(postCodeInfo, 10, 265, 120, 30);
		addComponent(postCode, 135, 265, 100, 30);
		addComponent(phoneInfo, 250, 265, 120, 30);
		addComponent(phoneNumber, 385, 265, 100, 30);

		emailInfo = new JLabel("Email");
		addComponent(emailInfo, 10, 300, 120, 30);
		email = new JTextField();
		addComponent(email, 135, 300, 100, 30);
		info = new JLabel(
				"Podaj dane dotyczące paczki, wszystkie dane są obowiązkowe.");
		addComponent(info, 0, 0, 600, 20);

		confirm = new JButton("Dodaj");
		addComponent(confirm, 150, 355, 100, 30);
		cancel = new JButton("Anuluj");
		addComponent(cancel, 300, 355, 100, 30);

		cancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				cancel();
			}
		});

		calculatePrice.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try {
					price.setText(String.valueOf(parcel.calculatePrice(
							Integer.parseInt(mass.getText()),
							Integer.parseInt(width.getText()),
							Integer.parseInt(length.getText()),
							Integer.parseInt(height.getText()))));
				} catch (NumberFormatException e1) {
					price.setText("n.a");
				} catch (NullPointerException e1) {
					price.setText("n.a");
				}
			}
		});
		
		confirm.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if (!editMode)
				addParcel();
				else
					editParcel();
			}
		});

	}

	private void addComponent(JComponent comp, int x, int y, int width,
			int height) {
		add(comp);
		comp.setBounds(x, y, width, height);
	}
	

	public void editParcelMode(Client c, Parcel p){
		parcel = p;
		editMode = true;
		sendDate.setText(p.getDate());
		parcelType.setSelectedItem(String.valueOf(p.getType()));
		parcelType.setEnabled(false);
		width.setText(String.valueOf(p.getWidth()));
		width.setEditable(false);
		height.setText(String.valueOf(p.getHeight()));
		height.setEditable(false);
		length.setText(String.valueOf(p.getLength()));
		length.setEditable(false);
		mass.setText(String.valueOf(p.getMass()));
		mass.setEditable(false);
		price.setText(String.valueOf(p.getPrice()));
		name.setText(c.getFirstName());
		surname.setText(c.getLastName());
		phoneNumber.setText(c.getPhoneNumber());
		street.setText(c.getAddress().getStreet());
		houseNumber.setText(String.valueOf(c.getAddress().getHouseNumber()));
		flatNumber.setText(String.valueOf(c.getAddress().getFlatNumber()));
		city.setText(c.getAddress().getCity());
		postCode.setText(c.getAddress().getPostCode());
	}
	
	private void editParcel(){
		errorInfo.setVisible(false);
		try {
			int clientId = communication.addNewClient(
					"",
					surname.getText(),
					name.getText(),
					email.getText(),
					phoneNumber.getText(),
					new Address(street.getText(), Integer.parseInt(houseNumber
							.getText()),
							Integer.parseInt(flatNumber.getText()), city
									.getText(), postCode.getText()));
			parcel.setRecipient(clientId);
			communication.modifyParcel(parcel);
			parent.registerMsg(1,parcel.getParcelID());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private boolean addParcel() {
		try {
			errorInfo.setVisible(false);
			int clientId = communication.addNewClient(
					"",
					surname.getText(),
					name.getText(),
					email.getText(),
					phoneNumber.getText(),
					new Address(street.getText(), Integer.parseInt(houseNumber
							.getText()),
							Integer.parseInt(flatNumber.getText()), city
									.getText(), postCode.getText()));
			int parcelNumber = communication.addParcel(sendDate.getText(),
					String.valueOf(parcelType.getSelectedItem()), 1,
					personData.getId(), clientId,
					Integer.parseInt(width.getText()),
					Integer.parseInt(height.getText()),
					Integer.parseInt(length.getText()),
					Integer.parseInt(mass.getText()),
					Integer.parseInt(price.getText()));
			
			if (parcelNumber!=-1) {
				parent.registerMsg(1,parcelNumber);
				
			}
		} catch (NumberFormatException e) {
			errorInfo
					.setText("Podane dane są nieprawidłowe, popraw je i spróbuj jeszcze raz");
			errorInfo.setVisible(true);
		} catch (NullPointerException e) {
			errorInfo.setText("Nie wypełniono wszystkich pól");
			errorInfo.setVisible(true);
		} catch (SQLException e) {
			errorInfo.setText("Niepoprawne dane odbiorcy");
			errorInfo.setVisible(true);
		}
		return true;
	}

	public void clearFields() {
		sendDate.setText(df.format(new Date()));
		width.setText("");
		height.setText("");
		length.setText("");
		mass.setText("");
		price.setText("");
		name.setText("");
		surname.setText("");
		phoneNumber.setText("");
		street.setText("");
		houseNumber.setText("");
		flatNumber.setText("");
		city.setText("");
		postCode.setText("");
	}

	private void cancel() {
		parent.registerMsg(1);
	}

	private ClientHomePage parent;
	// Pola
	private JFormattedTextField sendDate;
	private JComboBox parcelType;
	private JTextField sender;
	private JTextField width;
	private JTextField height;
	private JTextField length;
	private JTextField mass;
	private JTextField price;
	private JTextField name;
	private JTextField surname;
	private JTextField phoneNumber;
	private JTextField street;
	private JTextField houseNumber;
	private JTextField flatNumber;
	private JTextField city;
	private JTextField postCode;
	// Label
	private JLabel sendDateInfo;
	private JLabel parcelTypeInfo;
	private JLabel senderInfo;
	private JLabel widthInfo;
	private JLabel heightInfo;
	private JLabel lengthInfo;
	private JLabel massInfo;
	private JLabel priceInfo;
	private JLabel nameInfo;
	private JLabel surnameInfo;
	private JLabel phoneInfo;
	private JLabel streetInfo;
	private JLabel houseInfo;
	private JLabel flatInfo;
	private JLabel cityInfo;
	private JLabel postCodeInfo;
	private JButton calculatePrice;
	private JLabel info;
	private JButton confirm;
	private JButton cancel;
	private JLabel errorInfo;
	private JLabel emailInfo;
	private JTextField email;

	// Do daty
	private DateFormat df;
	private Parcel parcel;
	private Communication communication;
	private PersonData personData;
	private boolean editMode;
}
